﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using System.Web.UI.WebControls;
using Microsoft.SharePoint.Administration;

namespace GoogleAnalyticsSolution
{
    public class ManageGoogleAnalytics : Microsoft.SharePoint.WebControls.LayoutsPageBase
    {

        protected Label lblStatus;
        protected DropDownList ddlZone;
        protected Label lblZoneUrls;
        protected RadioButton rbInherit;
        protected RadioButton rbOverride;
        protected RadioButton rbDisable;
        protected TextBox tbWebTrackerCode;
        protected Label lblParentCode;

        protected Label lblParentTA;
        protected RadioButton rbTAInherit;
        protected RadioButton rbTAEnabled;
        protected RadioButton rbTADisabled;

        protected Label lblParentTE;
        protected RadioButton rbTEInherit;
        protected RadioButton rbTEEnabled;
        protected RadioButton rbTEDisabled;

        protected override void OnLoad(EventArgs e)
        {
            if (!IsPostBack && SPContext.Current != null && SPContext.Current.Web != null)
            {
                foreach (string zonename in Enum.GetNames(typeof(Microsoft.SharePoint.Administration.SPUrlZone)))
                {
                    ddlZone.Items.Add(zonename);
                }
                SPUrlZone currentzone = SPContext.Current.Site.Zone;
                ListItem currentzoneitem = ddlZone.Items.FindByText(Enum.GetName(typeof(Microsoft.SharePoint.Administration.SPUrlZone), currentzone));
                if (currentzoneitem != null)
                {
                    currentzoneitem.Selected = true;
                }

                ShowData();

            }

            base.OnLoad(e);
        }

        public void ShowData()
        {
            SPSite site = SPContext.Current.Site;
            SPWeb web = SPContext.Current.Web;
            SPUrlZone currentzone = (SPUrlZone)Enum.Parse(typeof(SPUrlZone), ddlZone.SelectedValue);

            lblZoneUrls.Text = "";
            foreach (SPAlternateUrl alturl in SPContext.Current.Site.WebApplication.AlternateUrls)
            {
                if (alturl.UrlZone.Equals(currentzone))
                {
                    if (lblZoneUrls.Text.Length > 0) lblZoneUrls.Text += ", ";
                    lblZoneUrls.Text += alturl.IncomingUrl;
                }
            }

            #region TrackerCode
            tbWebTrackerCode.Text = string.Empty;
            rbInherit.Checked = true;
            rbDisable.Checked = false;
            rbOverride.Checked = false;

            string ParentCode = GoogleAnalyticsLoader.GetTrackerCode(site, web.ParentWeb, currentzone, true);
            if (ParentCode == null)
            {
                lblParentCode.Text = "No TrackerCode has been set on ParentWebs";
            }
            else if (ParentCode == string.Empty)
            {
                lblParentCode.Text = "The TrackerCode has been set to Disabled on ParentWebs";
            }
            else
            {
                lblParentCode.Text = ParentCode;
            }

            tbWebTrackerCode.Text = string.Empty;
            string TrackerCode = GoogleAnalyticsLoader.GetTrackerCode(site, web, currentzone, false);

            if (TrackerCode == string.Empty)
            {
                rbDisable.Checked = true;
            }
            else if (TrackerCode == null)
            {
                // rbInherit.Checked = true;
            }
            else
            {
                rbOverride.Checked = true;
                tbWebTrackerCode.Text = TrackerCode;
            }
            #endregion

            #region TrackAuthenticated
            rbTAInherit.Checked = true;
            rbTADisabled.Checked = false;
            rbTAEnabled.Checked = false;

            bool? TAParentValue = GoogleAnalyticsLoader.GetTrackAuthenticated(site, web.ParentWeb, currentzone, true);
            if (!TAParentValue.HasValue)
            {
                lblParentTA.Text = "This setting is not set on ParentWebs";
            } else
            {
                if (TAParentValue.Value)
                {
                    lblParentTA.Text = string.Format("This setting has been set to \"{0}\" on ParentWebs", rbTAEnabled.Text);
                }
                else
                {
                    lblParentTA.Text = string.Format("This setting has been set to \"{0}\" on ParentWebs", rbTADisabled.Text);
                }
            }            

            bool? TAValue = GoogleAnalyticsLoader.GetTrackAuthenticated(site, web, currentzone, false);

            if (TAValue.HasValue)
            {
                if (TAValue.Value)
                {
                    rbTAEnabled.Checked = true;
                }
                else
                {
                    rbTADisabled.Checked = true;
                }
            }
            #endregion

            #region TrackEditMode
            rbTEInherit.Checked = true;
            rbTEDisabled.Checked = false;
            rbTEEnabled.Checked = false;

            bool? TEParentValue = GoogleAnalyticsLoader.GetTrackEditMode(site, web.ParentWeb, currentzone, true);
            if (!TEParentValue.HasValue)
            {
                lblParentTE.Text = "This setting is not set on ParentWebs";
            }
            else 
            {
                if (TEParentValue.Value)
                {
                    lblParentTE.Text = string.Format("This setting has been set to \"{0}\" on ParentWebs", rbTEEnabled.Text);
                }
                else
                {
                    lblParentTE.Text = string.Format("This setting has been set to \"{0}\" on ParentWebs", rbTEDisabled.Text);
                }
            }

            bool? TEValue = GoogleAnalyticsLoader.GetTrackEditMode(site, web, currentzone, false);

            if (TEValue.HasValue)
            {
                if (TEValue.Value)
                {
                    rbTEEnabled.Checked = true;
                }
                else
                {
                    rbTEDisabled.Checked = true;
                }
            }
            #endregion
        }

        public void ZoneChanged(object sender, EventArgs e)
        {
            ShowData();
        }


        public void SaveClick(object sender, EventArgs e)
        {
            if (SPContext.Current != null && SPContext.Current.Web != null)
            {
                SPUrlZone zone = (SPUrlZone)Enum.Parse(typeof(SPUrlZone), ddlZone.SelectedValue);
                SPWeb web = SPContext.Current.Web;
                if (rbOverride.Checked)
                {
                    GoogleAnalyticsLoader.SetTrackerCode(web, zone, tbWebTrackerCode.Text);
                }
                else if (rbDisable.Checked)
                {
                    GoogleAnalyticsLoader.SetTrackerCode(web, zone, string.Empty);
                }
                else if (rbInherit.Checked)
                {
                    GoogleAnalyticsLoader.SetTrackerCode(web, zone, null);
                }

                if (rbTADisabled.Checked)
                {
                    GoogleAnalyticsLoader.SetTrackAuthenticated(web, zone, false);
                }
                else if (rbTAEnabled.Checked)
                {
                    GoogleAnalyticsLoader.SetTrackAuthenticated(web, zone, true);
                }
                else
                {
                    GoogleAnalyticsLoader.SetTrackAuthenticated(web, zone, null);
                }

                if (rbTEDisabled.Checked)
                {
                    GoogleAnalyticsLoader.SetTrackEditMode(web, zone, false);
                }
                else if (rbTEEnabled.Checked)
                {
                    GoogleAnalyticsLoader.SetTrackEditMode(web, zone, true);
                }
                else
                {
                    GoogleAnalyticsLoader.SetTrackEditMode(web, zone, null);
                }

                web.Update();

                lblStatus.Text = "Saved...";
                ShowData();
            }

        }
    }
}
